<?php
namespace Home\Model;

use Think\Model;

class OrderModel extends Model{
     /**
      * 查詢訂單
      * @param string $where
      * @return unknown
      */
    public function find_order($where = null,$type = null){
        if($type ==null || $type =="select"){
            $result = $this->alias('as o')->
            join(C('DB_PREFIX').'order_info as oi ON o.ordersn = oi.ordersn')->
            join(C('DB_PREFIX').'user_car as uc ON oi.user_car_id = uc.id')->
            join(C('DB_PREFIX').'user as u ON oi.openid = u.openid')->
            where($where)->order('o.wash_status asc')->select();
        }
        else if($type=='find'){
            $result = $this->alias('as o')->
            join(C('DB_PREFIX').'order_info as oi ON o.ordersn = oi.ordersn')->
            join(C('DB_PREFIX').'user_car as uc ON oi.user_car_id = uc.id')->
            join(C('DB_PREFIX').'user as u ON oi.openid = u.openid')->
            where($where)->order('o.wash_status asc')->find();
        }

        return $result;
    }


    /**
     * [my_order_list 按条件搜索我的订单列表]
     * @author xiezy
     * @date   2017-09-11
     * @param  [type]     $where [条件是否为空] 统一查询 【订单，订单详情，我的爱车，店铺】
     * @param  [type]     $group [分组条件]
     * @param  [type]     $order [排序条件]
     * @return [type]            [返回查询结果]
     */
    public function my_order_list($where = null,$field = null,$group=null,$order=null){
      if($where!=null){
            $this->alias('as o')
              ->join(C('DB_PREFIX').'order_info as oi ON o.ordersn = oi.ordersn')
              ->join(C('DB_PREFIX').'user_car as uc ON oi.user_car_id = uc.id')
              ->join(C('DB_PREFIX').'server_address as sa ON sa.id= o.server_address_id')
              // ->join(C('DB_PREFIX').'coupon as c ON c.id= o.coupon_id')
              ->where($where);
            if ($field!=null) {
              $this->field($field);
            }
            if ($group!=null) {
              $this->group($group);
            }
            if ($order!=null) {
              $this->order($order);
            }
            $result = $this->select();
        }
        return $result;
    }
}
